/**
* 版权声明：厦门睿商网络科技有限公司 版权所有 违者必究
* 日    期：2020-04-27
*/
package com.rzico.basics.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.rzico.basics.model.CardView;
import com.rzico.basics.service.CardService;
import com.rzico.annotation.Log;
import com.rzico.base.BaseController;
import com.rzico.base.CommResult;
import com.rzico.basics.entity.Card;
import com.rzico.basics.entity.Enterprise;
import com.rzico.basics.entity.Member;
import com.rzico.basics.service.EnterpriseService;
import com.rzico.basics.service.MemberService;
import com.rzico.core.service.SysSequenceService;
import com.rzico.entity.PageResult;
import com.rzico.entity.Pageable;
import com.rzico.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 会员卡控制层
 *
 * @author Rzico Boot
 * @version 1.0
 * @date 2020-04-27
 */
@Api(description = "会员卡接口")
@RestController
@RequestMapping("/admin/card")
public class CardAdminController extends BaseController {

    @Autowired
    private CardService cardService;

    @Autowired
    private EnterpriseService enterpriseService;


    @Autowired
    private SysSequenceService sysSequenceService;


    @Autowired
    private MemberService memberService;

    /**
     * 分页查询会员卡
     *
     * @return
    */
    @ApiOperation("分页查询会员卡")
    @GetMapping("/list")
    @ApiImplicitParams(
            {    @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "String", paramType = "query") ,
                    @ApiImplicitParam(name = "cardType", value = "类型 {0:充值卡,1:福利卡}", dataType = "String", paramType = "query")
    })
    public CommResult<Card> list(Long memberId,Integer cardType, Pageable pageable) {
        Map<String, Object> params = new HashMap<String, Object>();
        params = buildSortField(params, pageable);


        Member member = memberService.selectByPrimaryKey(memberId);
        params.put("memberId",member.getId());
        if (cardType!=null) {
            params.put("cardType",cardType);
        }

        if (cardType==1) {
            params.put("expiredDate", DateUtils.addDays(new Date(), -7));
        }

        Page<Object> startPage = PageHelper.startPage(pageable.getPageNum(), pageable.getPageSize());
        List<Card> list = cardService.selectList(params);
        PageResult<Card> pageResult = new PageResult<Card>(list, startPage.getTotal(), pageable);
        return CommResult.success(pageResult);
    }

    /**
     * 会员卡/福利卡余额
     *
     * @return
     */
    @ApiOperation("会员卡/福利卡余额")
    @GetMapping("/summary")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "memberId", value = "会员ID", dataType = "Long", paramType = "query")
    })
    public CommResult<CardView> summary(Long memberId) {
        Member member = memberService.selectByPrimaryKey(memberId);
        CardView cardVo = cardService.summary(member.getId());
        return CommResult.success(cardVo);

    }

    /**
     * 批量删除会员卡
     *
     * @param ids
     * @return
     */
    @Log(desc = "批量删除会员卡", type = Log.LOG_TYPE.DEL)
    @ApiOperation("批量删除会员卡,ids用逗号拼接")
    @PostMapping("/del/{ids}")
    public CommResult<Card> del(@PathVariable String ids) {

        cardService.deleteByIds(ids.split(","));
        return CommResult.success();
    }

    /**
     * 获取企业所有批次卡
     *
     * @return
     */
    @ApiOperation("获取企业所有批次卡")
    @GetMapping("/getComapnyCard")
    @ApiImplicitParams(
            {    @ApiImplicitParam(name = "companyId", value = "企业ID", dataType = "String", paramType = "query") ,
                    @ApiImplicitParam(name = "cardType", value = "类型 {0:充值卡,1:福利卡}", dataType = "String", paramType = "query")
            })
    public CommResult<Card> getComapnyCard(Long companyId,Integer cardType) {
        Map<String, Object> params = new HashMap<String, Object>();

        Enterprise enterprise = enterpriseService.getCurrent();
        if (enterprise!=null) {
            params.put("enterpriseId", enterprise.getId());
        } else {
            return CommResult.error("没有开通企业");
        }

        params.put("companyId",companyId);

        params.put("cardType", cardType);

        List<Card> list = cardService.getComapnyCard(params);
        return CommResult.success(list);
    }


}
